package com.controller;

import com.bean.Student;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {

    @GetMapping("/test")
    public String HelloSpringMVC(@RequestParam("p1") String p1,@RequestParam("u1") String u1) {
        System.out.println(p1 + "u1 = " + u1);
        return "hello.jsp";
    }




    @GetMapping(value = "/json", produces={"application/json; charset=UTF-8"})
    @ResponseBody
    public String objToJsonStr() {

       Student s = new Student();
       s.setAge(20);
       s.setName("张三");
       s.setSno(1);

        ObjectMapper map = new ObjectMapper();
        try {
           return map.writeValueAsString(s);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
